#include "pm/dbobject.h"

using namespace db;

Table::Table()
{
}

Table::Table(wxSQLite3ResultSet& res)
{
	load(res);
}

bool Table::load(wxSQLite3ResultSet& res)
{
	return m_id.load(res, wxT("id"), db::ID::undefined);
}

ID::value_t Table::last_insert_id(wxSQLite3Database& db)
{
	return db.ExecuteScalar("select last_insert_rowid()");
}

bool Table::insert(wxSQLite3Database& db)
{
	if (valid()) {
		return false;
	}
	update_value_t res = prepare_insert(db);
	if (res.first == false) {
		return false;
	}
	if (res.second.ExecuteUpdate() != 1) {
		return false;
	}
	m_id( last_insert_id(db) );
	return true;
}

bool Table::update(wxSQLite3Database& db)
{
	if (!valid()) {
		return false;
	}
	update_value_t res = prepare_update(db);
	return res.first && (res.second.ExecuteUpdate() == 1);
}
